import LinkListNode, { LinkList } from "../../Utils/LinkList";
import { ShutdownType } from "./ShutdownType";
import {AzraelFrameWorkComponent} from "./AzraelFrameWork";

export class GameEntry {
    static readonly GameFrameworkSceneId: number = 0;
    private static readonly s_AzraelFrameWorkComponents: LinkList<AzraelFrameWorkComponent> = new LinkList();
    public static getComponment<T extends AzraelFrameWorkComponent>(type:{prototype:T}): T {
        let current: LinkListNode<AzraelFrameWorkComponent> = GameEntry.s_AzraelFrameWorkComponents.First;
        while (current != null) {
            let anytype:any = type;
            if (current.Value instanceof anytype) {
                return current.Value as T;
            }
            current = current.Next;
        }
        return null;
    }
    public static RegisterComponent(azraelFrameWorkComponent: AzraelFrameWorkComponent): void {
        if (azraelFrameWorkComponent == null) {
            cc.error("Game Framework component is invalid");
            return;
        }
        let current: LinkListNode<AzraelFrameWorkComponent> = GameEntry.s_AzraelFrameWorkComponents.First;
        while (current != null) {
            let className:string = cc.js.getClassName(current.Value);
            let newclassName:string = cc.js.getClassName(azraelFrameWorkComponent);
            if (className === newclassName) {
                return;
            }
            current = current.Next;
        }
        GameEntry.s_AzraelFrameWorkComponents.AddLast(azraelFrameWorkComponent);
    }

    private static getBaseComPonent(){
        let current: LinkListNode<AzraelFrameWorkComponent> = GameEntry.s_AzraelFrameWorkComponents.First;
        while (current != null) {
            let className:string = cc.js.getClassName(current.Value);
            if (className === "BaseComponent") {
                return current.Value;
            }
            current = current.Next;
        }
        return null;
    }
    public static Shutdown(shutdownType: ShutdownType): void {
        let baseComponment: any = GameEntry.getBaseComPonent();
        if (baseComponment != null) {
            baseComponment.ShutDown();
        }
        GameEntry.s_AzraelFrameWorkComponents.Clear();
        if (shutdownType === ShutdownType.None) {
            return;
        }

        if (shutdownType === ShutdownType.Restart) {
            cc.game.restart();
        }

        if (shutdownType === ShutdownType.Quit) {
            cc.game.end();
        }
    }



}




